package net.xxcxcxcx.xc_enhancedmob.render.boss_bar.unit.type;

import net.minecraft.nbt.NbtCompound;
import net.xxcxcxcx.xc_enhancedmob.render.boss_bar.unit.type.cover.BossBarCoverType;
import net.xxcxcxcx.xc_enhancedmob.render.boss_bar.unit.type.cover.TestBossBarCoverType;
import net.xxcxcxcx.xc_enhancedmob.render.boss_bar.unit.type.reality.BossBarRealityType;
import net.xxcxcxcx.xc_enhancedmob.render.boss_bar.unit.type.reality.TestClientCustomBossBar;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

public class BossBarTypeHelper {
    public static List<Class> AllBossBarReality = new ArrayList<>();
    public static List<Class> AllBossBarCover = new ArrayList<>();

    public static void Init() {
        RealityInit();
        CoverInit();
    }

    public static void RealityInit() {
        AllBossBarReality.add(TestClientCustomBossBar.class);
    }

    public static void CoverInit() {
        AllBossBarCover.add(TestBossBarCoverType.class);
    }

    public static BossBarRealityType makeBossBarFromNbt(NbtCompound nbt) {
        if (nbt.contains("ID")) {
            int num = nbt.getInt("ID");
            if (num >= AllBossBarReality.size() || num == -1) return new TestClientCustomBossBar();
            Class aClass = AllBossBarReality.get(num);
            try {
                Constructor constructor = aClass.getConstructor(NbtCompound.class);
                return (BossBarRealityType) constructor.newInstance(nbt);
            } catch (NoSuchMethodException | IllegalAccessException | InstantiationException |
                     InvocationTargetException e) {
                throw new RuntimeException(e);
            }
        }
        return new TestClientCustomBossBar();
    }

    public static BossBarCoverType makeBossBarCoverFromNbt(NbtCompound nbt) {
        if (nbt.contains("ID")) {
            int num = nbt.getInt("ID");
            if (num >= AllBossBarReality.size() || num == -1) return new TestBossBarCoverType();
            Class aClass = AllBossBarReality.get(num);
            try {
                Constructor constructor = aClass.getConstructor(NbtCompound.class);
                return (BossBarCoverType) constructor.newInstance(nbt);
            } catch (NoSuchMethodException | IllegalAccessException | InstantiationException |
                     InvocationTargetException e) {
                throw new RuntimeException(e);
            }
        }
        return new TestBossBarCoverType();
    }
}
